package com.bjpowernode.singleton.attack;

import java.io.*;

/**
 * @ClassName Singleton06
 *
 * @Author Cat
 */
public final class SingletonAttack06 implements Serializable {

    private static SingletonAttack06 instance = null;

    static {
        instance = new SingletonAttack06();
    }

    private SingletonAttack06() {
        if (instance != null) {
            throw new RuntimeException("哎呀，不能反射创建单例对象.");
        }
    }

    public static SingletonAttack06 getInstance() {
        return instance;
    }

    public Object readResolve() {
        return instance;
    }

    public static void main(String[] args) throws Exception{
        /*SingletonAttack06 instance1 = SingletonAttack06.getInstance();
        System.out.println(instance1);*/

        /*Constructor<SingletonAttack06> constructor = SingletonAttack06.class.getDeclaredConstructor();
        constructor.setAccessible(true);//对私有构造器开放权限

        SingletonAttack06 newInstance1 = constructor.newInstance();
        System.out.println(newInstance1);

        SingletonAttack06 newInstance2 = constructor.newInstance();
        System.out.println(newInstance2);*/


        //序列化
        SingletonAttack06 instance1 = SingletonAttack06.getInstance();
        System.out.println(instance1);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("SerSingleton.obj"));
        oos.writeObject(instance1);
        oos.flush();
        oos.close();

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("SerSingleton.obj"));
        SingletonAttack06 rs01 = (SingletonAttack06)ois.readObject();
        ois.close();
        System.out.println(rs01);

    }
}